package com.yuke.cloud.service.omc.mq;


import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.omc.entity.DelayMq;
import com.yuke.cloud.service.omc.entity.OdsOrder;
import com.yuke.cloud.service.omc.entity.OdsOrderDelivery;
import com.yuke.cloud.service.omc.service.OdsOrderDeliveryService;
import com.yuke.cloud.service.omc.service.OdsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@Slf4j
public class DelayReceiver {
    @Autowired
    private WmcStorageFeignApi wmcStorageFeignApi;
    @Resource
    private OdsOrderDeliveryService odsOrderDeliveryService;
    @Resource
    private OdsOrderService odsOrderService;
    @Autowired
    private DelaySender delaySender;

    @RabbitListener(queues = {DelayRabbitConfig.ORDER_QUEUE_NAME})
    public void orderDelayQueue(DelayMq order, Message message, Channel channel) throws Exception {
        if (order == null) {
            return;
        }
        OdsOrderDelivery odsOrderDelivery = new OdsOrderDelivery();
        odsOrderDelivery.setOrderId(order.getOrderId());
        List<OdsOrderDelivery> select = odsOrderDeliveryService.select(odsOrderDelivery);
        if (select.size() == 0) {
            return;
        }
        Long expressCompanyId = select.get(0).getExpressCompanyId();
        String trackingNumber = select.get(0).getTrackingNumber();
        Map map = new HashMap();
        map.put("1", "sf");
        map.put("2", "ht");
        map.put("3", "zt");
        map.put("4", "yd");
        map.put("5", "sto");
        map.put("6", "yt");
        map.put("7", "wx");
        map.put("8", "zjs");
        map.put("9", "dd");
        map.put("10", "ems");
        Object o = map.get(expressCompanyId.toString());
        Map map1 = new HashMap();
        map1.put("trackingNumber", trackingNumber);
        map1.put("transferComponey", o);
        map1.put("shippingType", order.getShippingType());  // add by wg 20190323
        map1.put("sendTime", order.getSendTime());
        Object getlogistics = wmcStorageFeignApi.getlogistics(map1); // 查询订单的物流状态
        if (getlogistics == null || getlogistics.equals("")) {

            // todo 容错处理：如果返回为空则发送消息，延迟2天再进行查询,add by wg 20190219
            DelayMq delayMq = new DelayMq();
            delayMq.setDelaytime(2 * 24 * 60 * 60);
            delayMq.setOrderId(order.getOrderId());
            delayMq.setShippingType(order.getShippingType());  // add by wg 20190323
            delayMq.setSendTime(order.getSendTime());
            delaySender.sendDelay(delayMq);

            return;
        }
        //modify by zyq 20200603
        Map entity = JSON.parseObject(JSON.toJSONString(getlogistics),Map.class);

        List<Map> info = (List<Map>)entity.get("info");
        Object status = info.get(0).get("status");

        if (status.toString().equals("delivering") || status.toString().equals("signed")) {
            // add by wg 20190325 先获取订单状态，如果订单状态是1：由仓库发货时，则改变
            //用户已经收货或者快递已经送达快递点都算收货
            OdsOrder odsOrder = new OdsOrder();
            odsOrder.setOrderId(order.getOrderId());
            odsOrder.setShippingStatus((byte) 3);  //  已收货
            OdsOrder odsOrderTemp = odsOrderService.findById(order.getOrderId());

            if (PublicUtil.isNotEmpty(odsOrderTemp) && odsOrderTemp.getOrderStatus() == 1) {
                odsOrder.setOrderStatus((byte) 2);
            }
            odsOrderService.update(odsOrder);
        }else {
            DelayMq delayMq = new DelayMq();
            delayMq.setDelaytime(2 * 24 * 60 * 60);
            delayMq.setOrderId(order.getOrderId());
            delayMq.setShippingType(order.getShippingType());  // add by wg 20190323
            delayMq.setSendTime(order.getSendTime());
            delaySender.sendDelay(delayMq);
        }
    }
}
